package apptoko.com;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import apptoko.com.event.Event.UserInfoSuccessEvent;
import apptoko.com.util.Constants;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import apptoko.com.R;
import com.google.analytics.tracking.android.EasyTracker;

import de.greenrobot.event.EventBus;

public class RatingActivity extends SherlockActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setThemeLowEndDevice();
		setContentView(R.layout.activity_rating);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		EasyTracker.getInstance().setContext(this);
		Button doGuide=(Button)findViewById(R.id.doGuide);
		doGuide.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder=new Builder(RatingActivity.this);
				LayoutInflater factory = LayoutInflater.from(RatingActivity.this);
				final View view;
					view = factory.inflate(R.layout.help_get_extra_tym, null);
				builder.setView(view);
				builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				}).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
						EasyTracker.getTracker().sendEvent(Constants.EVENT,Constants.EVENT_ACTION, "go to CHPlay to rate AppStoreVn", Long.valueOf(1));
						gotoCHPLay();
					}
				});
				builder.show();
				
			}
		});
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getTracker().sendView("Get free Yellow TYM screen");
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		EventBus.getDefault().register(this);
	}
	public void onEventMainThread(UserInfoSuccessEvent event){
	}
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		EventBus.getDefault().unregister(this);
	}
	private void setThemeLowEndDevice(){
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.action_bar_home);
            bg.setTileModeX(TileMode.REPEAT);
            getSupportActionBar().setBackgroundDrawable(bg);
        }
	}
	private void gotoCHPLay() {
		final String appName = "apptoko.com";
		try {
		    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+appName)));
		} catch (android.content.ActivityNotFoundException anfe) {
		    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id="+appName)));
		}
	}
}
